<template>
  <view class="log-list-container">
    <view class="header-section">
      <text class="page-title">操作日志</text>
    </view>
    
    <view class="filter-section">
      <view class="filter-item">
        <input class="filter-input" placeholder="用户名" v-model="searchForm.username" />
      </view>
      <view class="filter-item">
        <input class="filter-input" placeholder="操作类型" v-model="searchForm.action" />
      </view>
      <view class="filter-item">
        <input class="filter-input" placeholder="资源类型" v-model="searchForm.resource" />
      </view>
      <view class="filter-item">
        <input class="filter-input" placeholder="IP地址" v-model="searchForm.ip" />
      </view>
      <view class="filter-item">
        <picker mode="date" :value="searchForm.startDate" @change="onStartDateChange">
          <view class="filter-input">{{ searchForm.startDate || '开始日期' }}</view>
        </picker>
      </view>
      <view class="filter-item">
        <picker mode="date" :value="searchForm.endDate" @change="onEndDateChange">
          <view class="filter-input">{{ searchForm.endDate || '结束日期' }}</view>
        </picker>
      </view>
      <button class="filter-button" @click="searchLogs">搜索</button>
      <button class="filter-button danger" @click="clearFilters">清空</button>
    </view>
    
    <view class="log-list">
      <view class="log-item" v-for="log in logList" :key="log.id">
        <view class="log-header">
          <text class="log-username">{{ log.username }}</text>
          <text class="log-time">{{ formatTime(log.createdAt) }}</text>
        </view>
        <view class="log-content">
          <text class="log-action">{{ log.action }}</text>
          <text class="log-resource">{{ log.resource }}</text>
        </view>
        <view class="log-details">
          <text class="log-ip">IP: {{ log.ip }}</text>
          <text class="log-user-agent">{{ log.userAgent }}</text>
        </view>
        <view class="log-actions">
          <button class="action-button small" @click="viewLogDetails(log)">详情</button>
          <button class="action-button small danger" @click="deleteLog(log)">删除</button>
        </view>
      </view>
    </view>
    
    <view class="pagination-section">
      <button class="pagination-button" :disabled="currentPage <= 1" @click="prevPage">上一页</button>
      <text class="page-info">第 {{ currentPage }} 页，共 {{ totalPages }} 页</text>
      <button class="pagination-button" :disabled="currentPage >= totalPages" @click="nextPage">下一页</button>
    </view>
    
    <view class="empty-state" v-if="logList.length === 0">
      <text class="empty-text">暂无操作日志</text>
    </view>
    
    <!-- 日志详情弹窗 -->
    <view class="modal" v-if="showLogDetailModal">
      <view class="modal-content large">
        <view class="modal-header">
          <text class="modal-title">日志详情</text>
          <button class="close-button" @click="closeLogDetailModal">×</button>
        </view>
        <view class="modal-body">
          <view class="log-detail-item">
            <text class="detail-label">用户ID:</text>
            <text class="detail-value">{{ currentLog.userId }}</text>
          </view>
          <view class="log-detail-item">
            <text class="detail-label">用户名:</text>
            <text class="detail-value">{{ currentLog.username }}</text>
          </view>
          <view class="log-detail-item">
            <text class="detail-label">操作类型:</text>
            <text class="detail-value">{{ currentLog.action }}</text>
          </view>
          <view class="log-detail-item">
            <text class="detail-label">资源类型:</text>
            <text class="detail-value">{{ currentLog.resource }}</text>
          </view>
          <view class="log-detail-item">
            <text class="detail-label">IP地址:</text>
            <text class="detail-value">{{ currentLog.ip }}</text>
          </view>
          <view class="log-detail-item">
            <text class="detail-label">User-Agent:</text>
            <text class="detail-value">{{ currentLog.userAgent }}</text>
          </view>
          <view class="log-detail-item">
            <text class="detail-label">操作时间:</text>
            <text class="detail-value">{{ formatTime(currentLog.createdAt) }}</text>
          </view>
          <view class="log-detail-item">
            <text class="detail-label">详情:</text>
            <text class="detail-value">{{ currentLog.details }}</text>
          </view>
        </view>
        <view class="modal-footer">
          <button class="action-button" @click="closeLogDetailModal">关闭</button>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { logApi } from '../../../src/services/system'

// 定义日志类型
interface Log {
  id: number
  userId: number
  username: string
  action: string
  resource: string
  ip: string
  userAgent: string
  details: string
  createdAt: string
}

// 搜索表单
const searchForm = ref({
  username: '',
  action: '',
  resource: '',
  ip: '',
  startDate: '',
  endDate: ''
})

// 日志列表
const logList = ref<Log[]>([])

// 分页信息
const currentPage = ref(1)
const pageSize = ref(10)
const totalPages = ref(1)
const totalLogs = ref(0)

// 是否显示日志详情弹窗
const showLogDetailModal = ref(false)

// 当前查看的日志
const currentLog = ref<Log>({
  id: 0,
  userId: 0,
  username: '',
  action: '',
  resource: '',
  ip: '',
  userAgent: '',
  details: '',
  createdAt: ''
})

// 页面加载时获取日志列表
onMounted(() => {
  fetchLogList()
})

// 获取日志列表
const fetchLogList = async () => {
  try {
    const params: any = {
      page: currentPage.value,
      pageSize: pageSize.value,
      ...searchForm.value
    }
    
    const response: any = await logApi.getLogList(params)
    logList.value = response.data.list || response.data
    totalLogs.value = response.data.total || logList.value.length
    totalPages.value = Math.ceil(totalLogs.value / pageSize.value)
    
    uni.showToast({
      title: '获取操作日志成功',
      icon: 'success'
    })
  } catch (error) {
    console.error('获取操作日志失败:', error)
    uni.showToast({
      title: '获取操作日志失败',
      icon: 'none'
    })
  }
}

// 搜索日志
const searchLogs = () => {
  currentPage.value = 1
  fetchLogList()
}

// 清空筛选条件
const clearFilters = () => {
  searchForm.value = {
    username: '',
    action: '',
    resource: '',
    ip: '',
    startDate: '',
    endDate: ''
  }
  currentPage.value = 1
  fetchLogList()
}

// 开始日期变化
const onStartDateChange = (e: any) => {
  searchForm.value.startDate = e.detail.value
}

// 结束日期变化
const onEndDateChange = (e: any) => {
  searchForm.value.endDate = e.detail.value
}

// 格式化时间
const formatTime = (time: string) => {
  return time ? time.replace('T', ' ').substring(0, 19) : ''
}

// 查看日志详情
const viewLogDetails = (log: Log) => {
  currentLog.value = { ...log }
  showLogDetailModal.value = true
}

// 关闭日志详情弹窗
const closeLogDetailModal = () => {
  showLogDetailModal.value = false
}

// 删除日志
const deleteLog = async (log: Log) => {
  uni.showModal({
    title: '确认删除',
    content: `确定要删除这条操作日志吗？`,
    success: async (res) => {
      if (res.confirm) {
        try {
          await logApi.deleteLog(log.id)
          
          uni.showToast({
            title: '删除日志成功',
            icon: 'success'
          })
          
          // 重新获取日志列表
          fetchLogList()
        } catch (error) {
          console.error('删除日志失败:', error)
          uni.showToast({
            title: '删除日志失败',
            icon: 'none'
          })
        }
      }
    }
  })
}

// 上一页
const prevPage = () => {
  if (currentPage.value > 1) {
    currentPage.value--
    fetchLogList()
  }
}

// 下一页
const nextPage = () => {
  if (currentPage.value < totalPages.value) {
    currentPage.value++
    fetchLogList()
  }
}
</script>

<style lang="scss" scoped>
.log-list-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 20rpx;
}

.header-section {
  background-color: #fff;
  padding: 30rpx;
  margin-bottom: 20rpx;
  border-radius: 10rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  
  .page-title {
    font-size: 36rpx;
    font-weight: bold;
    color: #333;
    text-align: center;
  }
}

.filter-section {
  background-color: #fff;
  border-radius: 10rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  display: flex;
  flex-wrap: wrap;
  gap: 20rpx;
  align-items: center;
}

.filter-item {
  flex: 1;
  min-width: 150rpx;
}

.filter-input {
  background-color: #f5f5f5;
  padding: 10rpx 20rpx;
  border-radius: 10rpx;
  font-size: 28rpx;
  width: 100%;
  box-sizing: border-box;
}

.filter-button {
  background-color: #4B0082;
  color: #fff;
  border: none;
  padding: 10rpx 20rpx;
  border-radius: 10rpx;
  font-size: 28rpx;
  
  &.danger {
    background-color: #f44336;
  }
}

.log-list {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
}

.log-item {
  background-color: #fff;
  border-radius: 10rpx;
  padding: 30rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
}

.log-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
  padding-bottom: 20rpx;
  border-bottom: 2rpx solid #f5f5f5;
}

.log-username {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.log-time {
  font-size: 24rpx;
  color: #999;
}

.log-content {
  display: flex;
  gap: 20rpx;
  margin-bottom: 20rpx;
}

.log-action {
  font-size: 28rpx;
  color: #4B0082;
  background-color: rgba(75, 0, 130, 0.1);
  padding: 5rpx 15rpx;
  border-radius: 20rpx;
}

.log-resource {
  font-size: 28rpx;
  color: #2196F3;
  background-color: rgba(33, 150, 243, 0.1);
  padding: 5rpx 15rpx;
  border-radius: 20rpx;
}

.log-details {
  display: flex;
  flex-direction: column;
  gap: 10rpx;
  margin-bottom: 20rpx;
}

.log-ip, .log-user-agent {
  font-size: 24rpx;
  color: #666;
}

.log-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10rpx;
}

.action-button {
  background-color: #4B0082;
  color: #fff;
  border: none;
  padding: 10rpx 20rpx;
  border-radius: 10rpx;
  font-size: 24rpx;
  
  &.small {
    padding: 8rpx 15rpx;
    font-size: 24rpx;
  }
  
  &.danger {
    background-color: #f44336;
  }
}

.pagination-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 30rpx;
  padding: 0 20rpx;
}

.pagination-button {
  background-color: #4B0082;
  color: #fff;
  border: none;
  padding: 10rpx 20rpx;
  border-radius: 10rpx;
  font-size: 28rpx;
}

.pagination-button[disabled] {
  background-color: #ccc;
}

.page-info {
  font-size: 28rpx;
  color: #666;
}

.empty-state {
  text-align: center;
  padding: 100rpx 0;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
}

.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-content {
  background-color: #fff;
  border-radius: 10rpx;
  width: 90%;
  max-width: 600rpx;
  
  &.large {
    max-width: 800rpx;
  }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx;
  border-bottom: 2rpx solid #f5f5f5;
}

.modal-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.close-button {
  background: none;
  border: none;
  font-size: 40rpx;
  color: #999;
  padding: 0;
  width: 50rpx;
  height: 50rpx;
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal-body {
  padding: 30rpx;
  max-height: 60vh;
  overflow-y: auto;
}

.log-detail-item {
  display: flex;
  margin-bottom: 20rpx;
  
  &:last-child {
    margin-bottom: 0;
  }
}

.detail-label {
  font-size: 28rpx;
  color: #333;
  width: 150rpx;
  font-weight: bold;
}

.detail-value {
  font-size: 28rpx;
  color: #666;
  flex: 1;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 20rpx;
  padding: 30rpx;
  border-top: 2rpx solid #f5f5f5;
}
</style>